﻿using System.Collections.Generic;
using System.Linq;
using System.Xml.Linq;
using CI.Models.Data;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace CI.Models.Tests
{
    [TestClass]
    public class XmlObjectParserTests
    {
        [TestMethod]
        public void Parse_Returns_Correct_Types()
        {
            var p = new XmlObjectParser(new XDocument());

            Assert.AreEqual(2, p.Parse("2"));
            Assert.AreEqual(2.1m, p.Parse("2.1"));
            Assert.AreEqual("2a", p.Parse("2a"));
        }

        public static XDocument LoadProduction()
        {
            return XDocument.Load("Production.xml");
        }

        [TestMethod]
        public void ConstructInstanceFromElement_Returns_Item()
        {
            var p = new RigidXmlObjectParser(new XDocument());

            Item item =
                p.ConstructInstanceFromElement(new XElement("Item", new XAttribute("typeID", "1"),
                                                            new XAttribute("typeName", "My Item"))) as Item;

            Assert.IsNotNull(item);
            Assert.AreEqual(typeof (Item), item.GetType());
            Assert.AreEqual(1, item.TypeID);
            Assert.AreEqual("My Item", item.TypeName);
        }

        [TestMethod]
        public void Load_Production_XML_Into_Object_List()
        {
            var p = new RigidXmlObjectParser(LoadProduction());
            p.AddListType(typeof(RAM));
            p.AddListType(typeof(Material));
            p.AddListType(typeof(PIN));
            var o = p.GetObjects<Item>();

            Assert.IsNotNull(o);
            Assert.IsFalse(o.Count() < 10);
        }

        [TestMethod]
        public void Find_Bloodclaw_Light_Missile_In_Production()
        {
            var p = new RigidXmlObjectParser(LoadProduction());
            p.AddListType(typeof (RAM));
            p.AddListType(typeof (Material));
            p.AddListType(typeof (PIN));
            var o = p.GetObjects<Item>();

            var bloodclaw = o.Where(i => i.TypeName == "Bloodclaw Light Missile").FirstOrDefault();

            Assert.IsNotNull(bloodclaw);
            Assert.IsNull(bloodclaw.Schematic);
            Assert.AreEqual(210, bloodclaw.TypeID);
            Assert.AreEqual(384, bloodclaw.GroupID);

            var blueprint = bloodclaw.Blueprint;

            Assert.IsNotNull(blueprint);

            var ram = blueprint.RAMs;
            var materials = bloodclaw.Materials;

            Assert.AreEqual(4, ram.Count());
            Assert.AreEqual(3, materials.Count());
        }

        [TestMethod]
        public void Find_Robotics_Has_PI_Model()
        {
            var p = new RigidXmlObjectParser(LoadProduction());
            p.AddListType(typeof (RAM));
            p.AddListType(typeof (Material));
            p.AddListType(typeof (PIN));
            var o = p.GetObjects<Item>();

            var robotics = o.Where(i => i.TypeID == 9848).FirstOrDefault();

            Assert.IsNotNull(robotics);
            Assert.IsNotNull(robotics.Schematic);
            Assert.IsNull(robotics.Blueprint);

            var materials = robotics.Materials;
            var pins = robotics.Schematic.PINs;

            Assert.AreEqual(2, materials.Count());
            Assert.AreEqual(8, pins.Count());
        }
    }
}